<?php
/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
class FCBaseGadget {

  /**
   * Gadget rendering js method
   * @var string
   */
  protected $method = 'renderOpenSocialGadget';
  
  /**
   * Associative array with skin parameters
   * ie: 'BORDER_COLOR' => '#cccccc', ...
   * @var Array
   */
  protected $skin_params = array();
  
  /**
   * Associative array containing gadget's view parameters
   * @var Array
   */
  protected $view_params = array();
  
  /**
   * Associative array containing gadget's parameters
   * @var Array
   */
  protected $gadget_params = array();
  
  /**
   * Associative array with attributes to be applied
   * to the container div
   * @var Array
   */
  protected $container_attributes = array();
  
  /**
   * id of the container div, automatically generated
   * @var string
   */
  protected $div_id;
  
  /**
   * site url, same as in your friendconnect account 
   * @var string
   */
  protected $site_url = '/';
  
  /**
   * constructor
   * @return void
   */
  public function __construct(){
    $this->div_id = uniqid('div_');
    $this->skin_params = $this->getSkin();
    $this->gadget_params = array('id'=> $this->div_id);
  }
  
  /**
   * Returns the default values for the base skin parameters
   * @see FCBaseGadget::setSkinParams
   * @return array
   */
  private function getSkin(){
    return array(
      'BORDER_COLOR' => '#cccccc',  
      'ENDCAP_BG_COLOR' => '#e0ecff',
      'ENDCAP_TEXT_COLOR' => '#333333',
      'ENDCAP_LINK_COLOR' => '#0000cc',
      'ALTERNATE_BG_COLOR' => '#ffffff',
      'CONTENT_BG_COLOR' => '#ffffff',
      'CONTENT_LINK_COLOR' => '#0000cc',
      'CONTENT_TEXT_COLOR' => '#333333',
      'CONTENT_SECONDARY_LINK_COLOR' => '#7777cc',
      'CONTENT_SECONDARY_TEXT_COLOR' => '#666666',
      'CONTENT_HEADLINE_COLOR' => '#333333'
    );
  }
  /**
   * Sets the skin attributes 
   * @param Array $skin_params associative array
   * @return void
   */
  protected function setSkinParams(Array $skin_params) {
    $this->skin_params = array_merge($this->skin_params, $skin_params);
  }
  
  /**
   * Sets the view parameters
   * @param Array $view_params aasociative array
   * @return void
   */
  protected function setViewParams($view_params) {
    $this->view_params = $view_params;
  }

  /**
   * Sets the gadget parameters
   * @param array $gadget_params
   * @return void
   */
  protected function setGadgetParams(Array $gadget_params) {
    $this->gadget_params = array_merge($this->gadget_params, $gadget_params);
  }
  /**
   * Sets the attributes to be applied to the container div
   * @param array $attributes
   * @return void
   */
  protected function setContainerAttributes(Array $attributes) {
    $this->container_attributes = $attributes;
  }
  
  /**
   * returns the gadget code
   * @return string
   */
  public function render(){
    
    $skin = $this->div_id . '_skin';
    if($this->view_params) {
      $this->gadget_params['view-params'] = $this->view_params;
    }
    $attrib = "";
    if($this->container_attributes) {
      foreach($this->container_attributes as $name => $value){
        $attrib .= $name.'="'.$value.'" ';
      }
    }

    $skin_json = json_encode($this->skin_params);
    $gadget_params_json = json_encode($this->gadget_params);
    return <<<GADGETCODE
    <div id="$this->div_id" $attrib></div>
    <script type="text/javascript">
      var {$skin} = {$skin_json};
      google.friendconnect.container.setParentUrl('{$this->site_url}');
      google.friendconnect.container.{$this->method}(
        {$gadget_params_json}, {$skin});
    </script>
GADGETCODE;
  }
}